Interop Forms Toolkit
How To Create a Form Event

Create a Form Event

Events defined in an InteropForm can be also exposed to Visual Basic 6.0.  To do so, decorate the custom event with the InteropFormEvent attribute.  Events must be defined as a delegate that is defined in the assembly. Event delegates must be explicitly defined.

<InteropFormEvent()> _
Public Event SampleEvent As SampleEventHandler

Public Delegate Sub SampleEventHandler(ByVal sampleEventText As String)

Defining events with implicit delegates (short-hand format where the delegate is automatically created) is not supported.

' do not define event with short-hand notation
Public Event SampleEvent(ByVal sampleEventText As String)

To quickly define an event use the InteropEvent snippet installed with the toolkit:  Simply type interopevent+Tab and complete the event signature.

Only certain Types may be used as delegate parameters.  See Type Restrictions for details.

 

Using System.EventArgs

There is an exception to the Type Restrictions for events.  Events may be defined with System.EventArgs as part of the delegate signature.  This is done because it is common to define an event using EventHandler as a the delegate.  The signature of System.EventHandler is shown below.

Public Delegate Sub EventHandler(ByVal sender As Object, ByVal e As System.EventArgs)

It is also common to create delegates that include parameters that are custom event argument classes that inherit from System.EventArgs. Defining events in this way should be considered an advanced topic for those comfortable with interop because the custom event argument class will need to properly be exposed to COM. An example is shown below:

Public Delegate Sub EventHandler(ByVal sender As Object, ByVal e As MyEventArgs)

Public Class MyEventArgs

    Inherits System.EventArgs

    ' custom implementation...

End Class

Note: To avoid COM interoperability issues, the signature exposed by the InteropForm will always be down cast to System.EventArgs.  Therefore, the COM client (Visual Basic 6.0 code) will need to cast the event arguments back to the correct type to work with them.